package com.example.gatewayzuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName TokenFilter
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2020/7/16 11:58
 * @Version 1.0
 **/
@Component
@Slf4j
public class LoggerFilter extends ZuulFilter {

    @Value("${message}")
    String  message;

    @Value("${password}")
    String password;


    /**
     * 过滤器的类型
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }


    /**
     * 过滤器执行的时机
     * @return
     */
    @Override
    public int filterOrder() {
        return 2;
    }


    /**
     * 判断是否进入过滤器拦截
     * @return
     */
    @Override
    public boolean shouldFilter() {
        //只拦截请求路径包含 /admin的请求   /hotel/admin/hotellist
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String url =request.getRequestURI();
        return url.contains("/admin/");
    }


    /**
     * 执行过滤器
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        log.info("============================日志拦截请求=============执行过滤操作=============="+message+"密码:"+password);
        return "pass";
    }
}
